package com.example.movie.ui.user.search.adapter

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.data.movie.model.vo.SearchVo
import com.example.movie.ui.movie.MovieDetailActivity
import com.example.movie.ui.user.search.SearchActivity
import kotlinx.android.synthetic.main.item_constellation.*
import kotlinx.android.synthetic.main.item_search.view.*

class SearchResultAdapter(private val items: List<SearchVo>, private val activity: SearchActivity) :
    RecyclerView.Adapter<SearchResultAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) :
        RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_search, parent, false)
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        with(holder.itemView) {
            items[position].apply {
                this@with.movie_name.text = this.movieName
                this@with.type.text = this.movieTypeName
                this@with.actors.text = this.actors
                this@with.hopeLookNum.text = this.hopeLook.toString() + "人想看"
                this@with.playDay.text = this.registerTime + "上映"

                Glide
                    .with(context)
                    .load(AppConstants.STATIC_SOURCE_URL + this.cimg)
                    .placeholder(R.mipmap.logo)
                    .error(R.mipmap.ic_launcher)
                    .into(this@with.movie_portrait as ImageView)

                this@with.wantSee.setOnClickListener{
                    Toast.makeText(context, "点击了想看", Toast.LENGTH_SHORT).show()
                }

                this@with.setOnClickListener {
                    with(Bundle()) {
                        putInt("movieId", this@apply.movieId)
                        val intent = Intent(activity, MovieDetailActivity::class.java)
                        intent.putExtras(this)
                        activity.startActivity(intent)
                    }
                }


            }

        }
    }

    override fun getItemCount() = items.size

}